package com.mmy.controller.sys;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mmy.controller.Common;
import com.mmy.domain.sys.TSysOrg;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.sys.TSysAddressServiceImpl;
import com.mmy.service.impl.sys.TSysUserServiceImpl;
import com.mmy.service.impl.sys.VSysAddressServiceImpl;
import com.mmy.service.permissionmenu.impl.PermissionServiceImpl;
import com.mmy.service.permissionmenu.impl.UserPermissionServiceImpl;
import com.mmy.service.sys.TSysOrgService;

/**
 * 类的介绍：控制层 登录注册
 *
 * @author
 * @date 创建日期
 * @version 1.0
 */
@Controller
public class TSysUserController {
	@Autowired
	TSysUserServiceImpl tSysUserServiceImpl;
	@Autowired
	TSysOrgService tSysOrgServiceImpl;
	@Autowired
	TSysAddressServiceImpl tSysAddressServiceImpl;
	@Autowired
	VSysAddressServiceImpl vSysAddressServiceImpl;
	@Autowired
	PermissionServiceImpl permissionServiceImpl;
	@Autowired
	UserPermissionServiceImpl userPermissionServiceImpl;
	@Autowired
	Common common;

	/* 进入前台 */
	/*
	 * @RequestMapping(value = "/index") public String toIndex() {
	 * 
	 * return "index"; }
	 */

	/* 进入后台用户首页 */
	@RequestMapping(value = "/memberindex")
	public String findAllMember(Model m) {
		List<TSysUser> db_TSysUser = tSysUserServiceImpl.findALLMember();
		m.addAttribute("newTSysUser", db_TSysUser);

		return "webbackground/membermanagement/memberindex";
	}

	/* 搜索游客 */
	@RequestMapping(value = "/searchmember")
	public String findMember(Model mSysUser, HttpServletRequest request) {
		String strUserName = request.getParameter("UserName");
		String strSex = request.getParameter("Sex");
		String strUserMobile = request.getParameter("UserMobile");
		List<TSysUser> db_TSysUser = null;
		if (strUserName == "") {
			if (strSex == "") {

				if (strUserMobile == "") {
					db_TSysUser = tSysUserServiceImpl.findALLMember();
				} else {
					db_TSysUser = tSysUserServiceImpl.findMemberByUserMobile(strUserMobile);
				}
			} else {
				if (strUserMobile == "") {
					db_TSysUser = tSysUserServiceImpl.findMemberBySex((byte) Integer.parseInt(strSex));
				} else {
					db_TSysUser = tSysUserServiceImpl.findMemberByUserMobileAndSex(strUserMobile,
							(byte) Integer.parseInt(strSex));
				}

			}
		} else {

			if (strSex == "") {

				if (strUserMobile == "") {
					db_TSysUser = tSysUserServiceImpl.findMemberByUserName(strUserName);
				} else {
					db_TSysUser = tSysUserServiceImpl.findMemberByUserNameAndUserMobile(strUserName, strUserMobile);
				}
			} else {
				if (strUserMobile == "") {
					db_TSysUser = tSysUserServiceImpl.findMemberByUserNameAndSex(strUserName,
							(byte) Integer.parseInt(strSex));
				} else {
					db_TSysUser = tSysUserServiceImpl.findMemberByUserNameAndUserMobileAndSex(strUserName,
							strUserMobile, (byte) Integer.parseInt(strSex));
				}

			}

		}

		mSysUser.addAttribute("newTSysUser", db_TSysUser);
		request.setAttribute("strUserName", strUserName);
		request.setAttribute("strUserMobile", strUserMobile);
		return "webbackground/membermanagement/memberindex";
	}

	/**
	 * 函数功能说明： 新建客户
	 *
	 * @param 参数说明 tSysUser
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.07.15
	 */
	@RequestMapping(value = "/tocreatemember")
	public String toCreateMember(Model mUser) {
		/* 查询所有旅行社 */
		List<TSysOrg> db_TSysOrg = tSysOrgServiceImpl.findAll();
		mUser.addAttribute("newTSysOrg", db_TSysOrg);
		return "webbackground/membermanagement/createmember";
	}

	@RequestMapping(value = "/savacreatemember")
	public String savaCreateMember(TSysUser tSysUser, HttpSession session) throws Exception {

		/* 通过查询登录名、邮箱、手机号、身份证 查询是否已注册 */
		String cUserAccount = tSysUser.getUserAccount();
		String cMail = tSysUser.getMail();
		String cUserMobile = tSysUser.getUserMobile();
		String cIdentity = tSysUser.getIdentity();
		/*
		 * if (tSysUserServiceImpl.findByUserAccount(cUserAccount).size() == 0 &&
		 * tSysUserServiceImpl.findByUserMobile(cUserMobile).size() == 0 &&
		 * tSysUserServiceImpl.findByEmail(cMail).size() == 0 &&
		 * tSysUserServiceImpl.findByIdentity(cIdentity).size() == 0) {
		 * 
		 * Object iUserID = tSysUserServiceImpl.getMaxMemberUserID(); if (iUserID ==
		 * null) { tSysUser.setUserID(100000000); } else {
		 * tSysUser.setUserID(Integer.parseInt(iUserID.toString()) + 1); }
		 */
		/*
		 * 创建时间为当前时间
		 */
		TSysUser tSysUserSession = (TSysUser) session.getAttribute("tSysUser");
		/* 初始化游客账号登录名和密码 */
		StringBuffer memberAccount = new StringBuffer();
		memberAccount.append("tourist");
		memberAccount.append(tSysUser.getUserID());
		String cMemberAccount = memberAccount.toString();
		tSysUser.setUserAccount(cMemberAccount);
		tSysUser.setPassWord("aa123456");
		tSysUser.setCreateDate(new Date());
		tSysUser.setCreator(tSysUserSession.getUserName());

		/* 注册 */
		tSysUserServiceImpl.save(tSysUser.getUserID());
		tSysUserServiceImpl.save(tSysUser);

		return "redirect:memberindex";

	}/*
		 * else
		 * 
		 * {
		 * 
		 * return "test";
		 * 
		 * }
		 */
	/*
	 * try {
	 * 
	 * String cUserMobile = tSysUser.getUserMobile();
	 * 
	 * if (tSysUserServiceImpl.findByUserMobile(cUserMobile).size() > 0) {
	 * 
	 * return "test"; } else { tSysUserServiceImpl.save(tSysUser);
	 * 
	 * return "redirect:memberindex";
	 * 
	 * } } catch (Exception e) { // TODO: handle exception } throw
	 * 
	 * return null;
	 */

	/**
	 * 函数功能说明： 修改客户
	 *
	 * @param 参数说明 tSysUser
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.07.15
	 */
	@RequestMapping("/updatemembercheckedfind")
	public String updateMemberCheckedFind(HttpServletRequest request, Model mMember) {

		String iUserID = request.getParameter("iUserID");

		List<TSysUser> db_TSysUser = tSysUserServiceImpl.findByiUserID(Integer.parseInt(iUserID));
		mMember.addAttribute("newTSysUser", db_TSysUser.get(0));
		return "webbackground/membermanagement/updatemember";
	}

	@RequestMapping(value = "/info")
	public String hh4() {
		return "webbackground/permission-menu/menu";
	}

	/**
	 * 函数功能说明： 修改用户
	 *
	 * @param 参数说明 tSysUser
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.07.15
	 */
	@RequestMapping("/updatestaffcheckedfind")
	public String updateStaffCheckedFind(HttpServletRequest request, Model mStaff) {

		String iUserID = request.getParameter("iUserID");

		List<TSysUser> db_TSysUser = tSysUserServiceImpl.findByiUserID(Integer.parseInt(iUserID));
		mStaff.addAttribute("newTSysUser", db_TSysUser.get(0));
		/* 查询所有旅行社 */
		List<TSysOrg> db_TSysOrg = tSysOrgServiceImpl.findAll();
		mStaff.addAttribute("newTSysOrg", db_TSysOrg);
		return "webbackground/staffmanagement/updatestaff";
	}

	/* 搜索用户 */
	@RequestMapping(value = "/searchstaff")
	public String findStaff(Model mSysUser, HttpServletRequest request) {
		String strUserName = request.getParameter("UserName");
		String strSex = request.getParameter("Sex");
		String strUserMobile = request.getParameter("UserMobile");
		List<TSysUser> db_TSysUser = null;
		if (strUserName == "") {
			if (strSex == "") {

				if (strUserMobile == "") {
					db_TSysUser = tSysUserServiceImpl.findALLStaff();
				} else {
					db_TSysUser = tSysUserServiceImpl.findStaffByUserMobile(strUserMobile);
				}
			} else {
				if (strUserMobile == "") {
					db_TSysUser = tSysUserServiceImpl.findStaffBySex((byte) Integer.parseInt(strSex));
				} else {
					db_TSysUser = tSysUserServiceImpl.findStaffByUserMobileAndSex(strUserMobile,
							(byte) Integer.parseInt(strSex));
				}

			}
		} else {

			if (strSex == "") {

				if (strUserMobile == "") {
					db_TSysUser = tSysUserServiceImpl.findStaffByUserName(strUserName);
				} else {
					db_TSysUser = tSysUserServiceImpl.findStaffByUserNameAndUserMobile(strUserName, strUserMobile);
				}
			} else {
				if (strUserMobile == "") {
					db_TSysUser = tSysUserServiceImpl.findStaffByUserNameAndSex(strUserName,
							(byte) Integer.parseInt(strSex));
				} else {
					db_TSysUser = tSysUserServiceImpl.findStaffByUserNameAndUserMobileAndSex(strUserName, strUserMobile,
							(byte) Integer.parseInt(strSex));
				}

			}

		}

		mSysUser.addAttribute("newTSysUser", db_TSysUser);
		request.setAttribute("strUserName", strUserName);
		request.setAttribute("strUserMobile", strUserMobile);
		return "webbackground/staffmanagement/staffindex";
	}

}
